package logic

import (
	"context"
	"doc-manage/models/mysql"
	"errors"

	"doc-manage/doc/doc"
	"doc-manage/doc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type DocUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDocUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DocUpdateLogic {
	return &DocUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改
func (l *DocUpdateLogic) DocUpdate(in *doc.DocUpdateRequest) (*doc.DocUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var Doc mysql.Doc
	if err := mysql.DB.Where("id=?", in.DocId).First(&Doc).Error; err != nil {
		return nil, errors.New("当前医生不存在，无法修改")
	}
	Doc = mysql.Doc{
		Model: gorm.Model{ID: uint(in.DocId)},
		Name:  in.Name,
		Image: in.Image,
		Area:  in.Area,
	}

	if err := Doc.DocUpdate(mysql.DB); err != nil {
		return nil, err
	}

	return &doc.DocUpdateResponse{
		DocId: int64(Doc.ID),
	}, nil
}
